package com.hr.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hr.entity.EduInfo;
import com.hr.service.EduInfoService;
import com.hr.utils.ConstantUtil;
import com.hr.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hr
 * @since 2021-10-15
 */
@CrossOrigin
@RestController
@RequestMapping("/hr/service/edu-info")
public class EduInfoController {

    @Autowired
    EduInfoService eduInfoService;

    @GetMapping("eduInfos")
    public R get_eduInfos() {
        QueryWrapper<EduInfo> eduInfoQueryWrapper = new QueryWrapper<>();
        eduInfoQueryWrapper.eq("user_id", ConstantUtil.CURRENT_USER);
        eduInfoQueryWrapper.orderByDesc("education");
        List<EduInfo> eduInfos = eduInfoService.list(eduInfoQueryWrapper);
        return R.ok().data("eduInfos", eduInfos);
    }

    @GetMapping("eduInfo")
    public R get_eduInfo(String id) {
        EduInfo eduInfo = eduInfoService.getById(id);
        return R.ok().data("eduInfo", eduInfo);
    }

    @PostMapping("eduInfo")
    public R add_eduInfo(@RequestBody EduInfo eduInfo) {
        eduInfo.setUserId(ConstantUtil.CURRENT_USER);
        boolean save = eduInfoService.save(eduInfo);
        return save ? R.ok() : R.error();
    }

    @PutMapping("eduInfo")
    public R update_eduInfo(@RequestBody EduInfo eduInfo) {
        eduInfo.setUserId(ConstantUtil.CURRENT_USER);
        boolean update = eduInfoService.updateById(eduInfo);
        return update ? R.ok() : R.error();
    }

    @DeleteMapping("eduInfo/{id}")
    public R delete_eduInfo(@PathVariable("id") Integer id) {
        boolean delete = eduInfoService.removeById(id);
        return delete ? R.ok() : R.error();
    }

}

